//
//  Product_Checkout1View.m
//  iFlower
//
//  Created by khanh nguyen on 7/26/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Product_Checkout1View.h"
#import "Product_Checkout2View.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "Request.h"
#import "Basket.h"
#import "LoginView.h"
#import "Shipping.h"
#import "Order.h"
#import "DeliveryOptions.h"

@implementation Product_Checkout1View

@synthesize radio1;
@synthesize radio2;
@synthesize radio3;
@synthesize radio4;
@synthesize delivery_method;
@synthesize basketTotal = basketTotal_;
@synthesize numberItem = numberItem_;
@synthesize discount = discount_;
@synthesize discountedSubtotal = discountedSubtotal_;
@synthesize delivery = delivery_;
@synthesize total = total_;
@synthesize textView1 = textView1_;
@synthesize textView2 = textView2_;
@synthesize btnRegister;

-(IBAction)button_Radio1:(id)sender
{
	radio1.imageView.hidden = NO;
	radio2.imageView.hidden = YES;
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setFlowers_for:@"E"];
}
-(IBAction)button_Radio2:(id)sender
{
	radio1.imageView.hidden = YES;
	radio2.imageView.hidden = NO;
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setFlowers_for:@"M"];
}
-(IBAction)button_Radio3:(id)sender
{
	radio3.imageView.hidden = NO;
	radio4.imageView.hidden = YES;
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setDeliver_to:@"E"];
}
-(IBAction)button_Radio4:(id)sender
{
	radio3.imageView.hidden = YES;
	radio4.imageView.hidden = NO;
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setDeliver_to:@"E"];
}

-(IBAction)button_Next:(id)sender
{
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setMessage:textView1_.text];	
	[[[iFlowerAppDelegate shareDelegate] deliveryOptions] setInstructions:textView2_.text];
	Product_Checkout2View * check2 = [[Product_Checkout2View alloc]initWithNibName:@"Product_Checkout2View" bundle:nil];
	[[[iFlowerAppDelegate shareDelegate] basket] setShippingFee: [[shippingData_ objectAtIndex:[pickerView selectedRowInComponent:0]] shippingFee]];
	[self.navigationController pushViewController:check2 animated:YES];
	[check2 release];
}
-(IBAction)buttonMenu:(id)sender
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}
-(IBAction)buttonBack:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
	
}


- (IBAction)buttonSelecDelivery:(id) sender
{
	actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
	[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
	CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
	pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
	pickerView.showsSelectionIndicator = YES;
	pickerView.dataSource = self;
	pickerView.delegate = self;
	[actionSheet addSubview:pickerView];
	//---
	UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
	closeButton.momentary = YES; 
	closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
	closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
	closeButton.tintColor = [UIColor blackColor];
	[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
	[actionSheet addSubview:closeButton];
	[closeButton release];
	
	[actionSheet showInView:self.view];
	
	[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
	
}
-(void)dismissActionSheet:(id)sender
{
	[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
	self.delivery_method.text = [[shippingData_ objectAtIndex:[pickerView selectedRowInComponent:0]] shippingName];	
	[[[iFlowerAppDelegate shareDelegate] order] setShipping:[shippingData_ objectAtIndex:[pickerView selectedRowInComponent:0]]];
}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/
- (void)updateItems
{
	float totalBasket = [[[iFlowerAppDelegate shareDelegate] basket] getBasketPrice];
	basketTotal_.text = [NSString stringWithFormat:@"£%3.2f",totalBasket];
	float fee = 0.0;
	if([pickerView selectedRowInComponent:0]>=0)
	{
		Shipping* s = [shippingData_ objectAtIndex:[pickerView selectedRowInComponent:0]];
		fee=s.shippingFee;
		delivery_.text = [NSString stringWithFormat:@"£%3.2f",s.shippingFee];
	}else {
		delivery_.text = @"£0.00";
	}
	discount_.text = [NSString stringWithFormat:@"£%3.2f",totalBasket/20];
	
	float totalAmount = fee+totalBasket*19/20;
	total_.text = [NSString stringWithFormat:@"£%3.2f",totalAmount];
	[[[iFlowerAppDelegate shareDelegate] basket] setTotalCost:totalBasket];
	[[[iFlowerAppDelegate shareDelegate] basket] setSubTotalCost:totalBasket*19/20];
	[[[iFlowerAppDelegate shareDelegate] basket] setFinalBasketPrice:totalAmount];
	[[[iFlowerAppDelegate shareDelegate] basket] setDiscount:totalBasket/20];
	discountedSubtotal_.text = [NSString stringWithFormat:@"£%3.2f",totalBasket*19/20];
	numberItem_.text = [NSString stringWithFormat:@"%d",[[[iFlowerAppDelegate shareDelegate] basket] count]];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//radio1.imageView.hidden = YES;
	radio2.imageView.hidden = YES;
	//radio3.imageView.hidden = YES;
	radio4.imageView.hidden = YES;
	//-----
	shippingData_ = [[NSMutableArray alloc] init];
	Shipping* shipObj = [[Shipping new] autorelease];
	shipObj.shippingId = 172;
	shipObj.shippingName = @"Next Day Delivery (£5.99)";
	shipObj.shippingFee = 5.99;
	[shippingData_ addObject:shipObj];
	Shipping* shipObj1 = [[Shipping new] autorelease];
	shipObj1.shippingId = 173;
	shipObj1.shippingName = @"Three Hour Delivery (£12.99)";
	shipObj1.shippingFee = 12.99;
	[shippingData_ addObject:shipObj1];
	Shipping* shipObj2 = [[Shipping new] autorelease];
	shipObj2.shippingId = 174;
	shipObj2.shippingName = @"Same Day Delivery (£7.99)";
	shipObj2.shippingFee = 7.99;
	[shippingData_ addObject:shipObj2];
	if (![[iFlowerAppDelegate shareDelegate] logined]) {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
	[[[iFlowerAppDelegate shareDelegate] order] setShipping:[shippingData_ objectAtIndex:0]];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self updateItems];
	if([[iFlowerAppDelegate shareDelegate] logined])
	{
		self.btnRegister.enabled = NO;
	}else {
		self.btnRegister.enabled = YES;
	}
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	self.btnRegister = nil;
}


- (void)dealloc {
	[btnRegister release];
	[textView2_ release];
	[textView1_ release];
	[delivery_ release];
	[discountedSubtotal_ release];
	[discount_ release];
	[total_ release];
	[basketTotal_ release];
	[numberItem_ release];
	[radio1 release];
	[radio2 release];
	[radio3 release];
	[radio4 release];
    [super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
	
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
	
	return [shippingData_ count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	
	return [[shippingData_ objectAtIndex:row] shippingName];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	
	NSLog(@"Selected Color: %@. Index of selected color: %i", [[shippingData_ objectAtIndex:row] shippingName], [[shippingData_ objectAtIndex:row] shippingId]);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([textView1_ isFirstResponder]) {
		[textView1_ resignFirstResponder];
	}
	if ([textView2_ isFirstResponder]) {
		[textView2_ resignFirstResponder];
	}
	[super touchesBegan:touches withEvent:event];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y - 120, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
	
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y + 120, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];	
}

@end
